package com.tanhua.db.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tanhua.api.UserApi;
import com.tanhua.db.mapper.UserMapper;
import com.tanhua.model.pojo.User;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * UserApi接口的实现类
 */
@DubboService
public class UserApiImpl implements UserApi {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User findByPhone(String phone) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getMobile,phone);
        User user = userMapper.selectOne(queryWrapper);
        return user;
    }

    @Override
    public Long save(User user) {
        userMapper.insert(user);
        return user.getId();
    }

    @Override
    public void updateUser(User user) {
        userMapper.updateById(user);
    }

    /**
     * 查询所有用户信息
     * @return
     */
    @Override
    public List<User> findAll() {
        return userMapper.selectList(null);
    }


    /**
     * 根据用户Id查询用户信息
     * @param userId
     * @return
     */
    @Override
    public User findByUserId(Long userId) {
        //构建查询条件
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getId,userId);
        return userMapper.selectOne(queryWrapper);
    }

    /**
     * 根据环信id查询用户信息
     * @param huanxinId
     * @return
     */
    @Override
    public User findByHuanXinId(String huanxinId) {
        //1.构建查询条件
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getHxUser,huanxinId);

        return userMapper.selectOne(queryWrapper);
    }
}
